
/*

  Program: 
  
  - This program prepares a .csv file of growth regression results to run in 
    Matlab for aggregate volatility calculations
   
	`1': name of output file (.csv format)
	`2': dummy "interaction" variable
		- 1 if specification = heterogeneous response
		- 0 if specification = benchmark
	`3': growth rate variable (used as LHS in "dGLM_1_microestimation.do")
	 
*/

capture program drop dGLM_9_matlab_prep
program define dGLM_9_matlab_prep

	* Create sector and sector-destination pair group identifiers 
	egen id_i=group($sect)
	egen id_ic=group($sect country)

	* In MatLab part, we need to calculate variance (over time) of firm-destination level growth rates
	* 	- Get rid of firm-destination pairs that are only defined over one specific year
	*	- Otherwise, variance is not properly defined as we are dividing over T-1 in volatility calculations
	egen cobs = count(`3'), by(id_ic id_fn)
	drop if cobs<2
	drop cobs
	
	* Create weights based on different levels of aggregation
	*	- firm-destination level (yearly)
	bys year: egen w_tot=sum(w_fn)
	gen w_fc=w_fn/w_tot
	drop w_tot
	
	drop id_i id_ic id_fn
	sort $sect country firmid year
	
	egen id_fc = group(firmid country)
	egen id_f = group(firmid)
	egen id_i = group($sect)
	egen id_ic = group($sect country)
	egen id_icy = group($sect country year)

	*	- firm level (yearly)
	egen w_f = sum(w_fc), by(id_f year)
	*	- sector-destination level (yearly)
	egen w_ic = sum(w_fc), by(id_ic year)
	*	- sector level (yearly)
	egen w_i = sum(w_fc), by(id_i year)
	*	- destination level (yearly)
	egen w_c = sum(w_fc), by(id_c year)

	* Order variables in specific order
	*	 - Importing of variables in Matlab is done in this specific order (!)
	
	* Specification: "benchmark"
	if `2'==0  {
	
		order id_fc id_f id_ic id_i id_c year id_icy `3' gr_idio gr_idio_com gr_idio_dest gr_sec gr_macro w_fc w_f w_ic w_i w_c  
		keep  id_fc id_f id_ic id_i id_c year id_icy `3' gr_idio gr_idio_com gr_idio_dest gr_sec gr_macro w_fc w_f w_ic w_i w_c  
	
	}
	
	* Specification: "heterogeneous response"
	if `2'==1  {	
	
		order id_fc id_f id_ic id_i id_c year id_icy `3' gr_idio gr_idio_com gr_idio_dest gr_sec gr_size gr_sizesec gr_macro w_fc w_f w_ic w_i w_c 
		keep  id_fc id_f id_ic id_i id_c year id_icy `3' gr_idio gr_idio_com gr_idio_dest gr_sec gr_size gr_sizesec gr_macro w_fc w_f w_ic w_i w_c  
	
	}
	
	* Specification: "local"
	if `2'==2  {	
	
		order id_fc id_f id_ic id_i id_c year id_icy `3' gr_idio gr_idio_com gr_idio_dest gr_sec gr_macro w_fc w_f w_ic w_i w_c gr_ze
		keep  id_fc id_f id_ic id_i id_c year id_icy `3' gr_idio gr_idio_com gr_idio_dest gr_sec gr_macro w_fc w_f w_ic w_i w_c gr_ze
	
	}

	* Sort at firm-destination level (across years)
	sort id_fc year

	* Export into .csv format for importing in Matlab
	outsheet using `1'.csv, comma replace

* End of .do file	
end
